﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.CompilerServices;

using LongNet.Threading;

namespace LongNet.Util
{
    /// <summary>
    /// Provides a set of static methods for arrays.
    /// </summary>
    public static class ThrowExtensions
    {
        #region Static Methods

        /// <summary>
        /// Validates that an <see cref="T:Object"/> is not null.
        /// </summary>
        /// <remarks>
        /// Validates that an <see cref="T:Object"/> is not null. If the <see cref="T:Object"/> is null,
        /// <see cref="T:ArgumentNullException"/> will be thrown.
        /// </remarks>
        /// <param name="value">The <see cref="T:Object"/> being extended.</param>
        /// <param name="paramName">The name of the parameter that caused the exception.</param>
        public static void ThrowsIfNull(this object value, string paramName)
        {
            if (value == null)
                throw new ArgumentNullException(paramName);
        }

        /// <summary>
        /// Validates that an <see cref="T:Object"/> is not null.
        /// </summary>
        /// <remarks>
        /// Validates that an <see cref="T:Object"/> is not null. If the <see cref="T:Object"/> is null,
        /// <see cref="T:ArgumentNullException"/> will be thrown.
        /// </remarks>
        /// <param name="value">The <see cref="T:Object"/> being extended.</param>
        /// <param name="paramName">The name of the parameter that caused the exception.</param>
        /// <param name="message">The error message that explains the reason for the required parameter.</param>
        public static void ThrowsIfNull(this object value, string paramName, string message)
        {
            if (value == null)
                throw new ArgumentNullException(paramName, message);
        }

        /// <summary>
        /// Validates that a <see cref="T:Disposable"/> object is not disposed.
        /// </summary>
        /// <remarks>
        /// Validates that a <see cref="T:Disposable"/> object is not disposed. If the object is disposed,
        /// <see cref="T:ObjectDisposedException"/> will be thrown.
        /// </remarks>
        /// <param name="value">The <see cref="T:Disposable"/> object being extended.</param>
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void ThrowsIfDisposed(this Disposable value)
        {
            if (value.IsDisposed)
            {
                StackFrame frame = new StackFrame(1, false);
                throw new ObjectDisposedException(frame.GetMethod().DeclaringType.Name);
            }
        }

        /// <summary>
        /// Validates that a <see cref="T:Disposable"/> object is not disposed.
        /// </summary>
        /// <remarks>
        /// Validates that a <see cref="T:Disposable"/> object is not disposed. If the object is disposed,
        /// <see cref="T:ObjectDisposedException"/> will be thrown.
        /// </remarks>
        /// <param name="value">The <see cref="T:Disposable"/> object being extended.</param>
        /// <param name="objectName">The type of the object that caused the exception.</param>
        public static void ThrowsIfDisposed(this Disposable value, string objectName)
        {
            if (value.IsDisposed)
                throw new ObjectDisposedException(objectName);
        }

        /// <summary>
        /// Validates that a <see cref="T:Disposable"/> object is not disposed.
        /// </summary>
        /// <remarks>
        /// Validates that a <see cref="T:Disposable"/> object is not disposed. If the object is disposed,
        /// <see cref="T:ObjectDisposedException"/> will be thrown.
        /// </remarks>
        /// <param name="value">The <see cref="T:Disposable"/> object being extended.</param>
        /// <param name="objectName">The name of the object that caused the exception.</param>
        /// <param name="message">The error message that explains the reason for the disposed object.</param>
        public static void ThrowsIfDisposed(this Disposable value, string objectName, string message)
        {
            if (value.IsDisposed)
                throw new ObjectDisposedException(objectName, message);
        }


        /// <summary>
        /// Validates that a string is not null and empty.
        /// </summary>
        /// <remarks>
        /// Validates that a string is not null and empty. If the string is null or empty,
        /// ArgumentException will be thrown.
        /// </remarks>
        /// <param name="value">The string being extended.</param>
        /// <param name="message">The error message that explains the reason for the required parameter.</param>
        public static void ThrowsIfNullOrEmpty(this string value, string message)
        {
            if (value == null || value.Length == 0)
                throw new ArgumentException(message);
        }

        /// <summary>
        /// Validates that a string is not null and empty.
        /// </summary>
        /// <remarks>
        /// Validates that a string is not null and empty. If the string is null or empty,
        /// ArgumentException will be thrown.
        /// </remarks>
        /// <param name="value">The string being extended.</param>
        /// <param name="paramName">The name of the parameter that caused the exception.</param>
        /// <param name="message">The error message that explains the reason for the required parameter.</param>
        public static void ThrowsIfNullOrEmpty(this string value, string paramName, string message)
        {
            if (value == null || value.Length == 0)
                throw new ArgumentException(message, paramName);
        }


        /// <summary>
        /// Validates that an array is not null and empty.
        /// </summary>
        /// <typeparam name="T">The type of items in array.</typeparam>
        /// <remarks>
        /// Validates that an array is not null and empty. If the array is null or empty,
        /// ArgumentException will be thrown.
        /// </remarks>
        /// <param name="value">The array being extended.</param>
        /// <param name="message">The error message that explains the reason for the required parameter.</param>
        public static void ThrowsIfNullOrEmpty<T>(this T[] value, string message)
        {
            if (value == null || value.Length == 0)
                throw new ArgumentException(message);
        }

        /// <summary>
        /// Validates that an array is not null and empty.
        /// </summary>
        /// <typeparam name="T">The type of items in array.</typeparam>
        /// <remarks>
        /// Validates that an array is not null and empty. If the array is null or empty,
        /// ArgumentException will be thrown.
        /// </remarks>
        /// <param name="value">The array being extended.</param>
        /// <param name="paramName">The name of the parameter that caused the exception.</param>
        /// <param name="message">The error message that explains the reason for the required parameter.</param>
        public static void ThrowsIfNullOrEmpty<T>(this T[] value, string paramName, string message)
        {
            if (value == null || value.Length == 0)
                throw new ArgumentException(message, paramName);
        }

        /// <summary>
        /// Validates that a collection is not null and empty.
        /// </summary>
        /// <typeparam name="T">The type of items in collection.</typeparam>
        /// <remarks>
        /// Validates that a collection is not null and empty. If the collection is null or empty,
        /// ArgumentException will be thrown.
        /// </remarks>
        /// <param name="value">The collection being extended.</param>
        /// <param name="message">The error message that explains the reason for the required parameter.</param>
        public static void ThrowsIfNullOrEmpty<T>(this ICollection<T> value, string message)
        {
            if (value == null || value.Count == 0)
                throw new ArgumentException(message);
        }

        /// <summary>
        /// Validates that a collection is not null and empty.
        /// </summary>
        /// <typeparam name="T">The type of items in collection.</typeparam>
        /// <remarks>
        /// Validates that a collection is not null and empty. If the collection is null or empty,
        /// ArgumentException will be thrown.
        /// </remarks>
        /// <param name="value">The collection being extended.</param>
        /// <param name="paramName">The name of the parameter that caused the exception.</param>
        /// <param name="message">The error message that explains the reason for the required parameter.</param>
        public static void ThrowsIfNullOrEmpty<T>(this ICollection<T> value, string paramName, string message)
        {
            if (value == null || value.Count == 0)
                throw new ArgumentException(message, paramName);
        }

        #endregion
    }
}
